package com.bawei.day16_messager_client;

import androidx.appcompat.app.AppCompatActivity;

import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Message;
import android.os.Messenger;
import android.os.RemoteException;

public class MainActivity extends AppCompatActivity {
    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            //TODO 1:向服务端发消息
            Messenger messenger = new Messenger(service);
            Message message=new Message();
            message.replyTo= messenger;
            Bundle bundle=new Bundle();
            bundle.putString("client_message","你好啊服务器");
            message.setData(bundle);
            try {
                messenger.send(message);
            } catch (RemoteException e) {
                e.printStackTrace();
            }





        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent=new Intent();
        intent.setAction("com.bawei.hh");
        intent.setPackage("com.bawei.day16_messager_server");
        bindService(intent,connection, Service.BIND_AUTO_CREATE);



    }
}
